package com.ltba.rf.view;

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import com.ltba.rf.control.RedFeatherManager;
import com.ltba.rf.control.file.AppData;
import com.ltba.rf.control.file.AppDataManager;

public class MainFrame extends JFrame implements ActionListener {

	private static final String START = "START";
	private static final String STOP = "STOP";
	private static final String UPDATE = "UPDATE";
	private static final boolean ENABLE = true;

	private void initComponents() {
		ImageIcon icon = new ImageIcon("pic/logo.jpg");
		browserPanel = new BrowserPanel();
		title = new JLabel(icon);
		jLabel1 = new JLabel("更新間隔");
		jLabel2 = new JLabel("警告震度");
		jLabel3 = new JLabel("警告音");
		intervalComb = new JComboBox();
		levelCombo = new JComboBox();
		soundComb = new JComboBox();

		controlBtn = new JButton("開始");
		controlBtn.setActionCommand(START);
		controlBtn.addActionListener(this);

		updateBtn = new javax.swing.JButton("更新");
		updateBtn.setActionCommand(UPDATE);
		updateBtn.addActionListener(this);

		intervalComb.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "３０秒","１分", "３分"}));
		levelCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"１度","２度", "３度", "４度", "５度" }));
		soundComb.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Rooster", "Ship Bell", "Siren", "Store Door" }));

	}

	private void initView() {

		javax.swing.GroupLayout titleLayout = new javax.swing.GroupLayout(title);
		title.setLayout(titleLayout);
		titleLayout.setHorizontalGroup(titleLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 173,
				Short.MAX_VALUE));
		titleLayout.setVerticalGroup(titleLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 313,
				Short.MAX_VALUE));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);

		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addComponent(
														title,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addGroup(
														javax.swing.GroupLayout.Alignment.LEADING,
														layout.createSequentialGroup()
																.addComponent(
																		jLabel1)
																.addGap(18, 18,
																		18)
																.addComponent(
																		intervalComb,
																		0, 107,
																		107))
												.addGroup(
														javax.swing.GroupLayout.Alignment.LEADING,
														layout.createSequentialGroup()
																.addComponent(
																		jLabel2)
																.addGap(18, 18,
																		18)
																.addComponent(
																		levelCombo,
																		0, 107,
																		107))
												.addGroup(
														javax.swing.GroupLayout.Alignment.LEADING,
														layout.createSequentialGroup()
																.addComponent(
																		jLabel3)
																.addGap(30, 30,
																		30)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						updateBtn,
																						javax.swing.GroupLayout.Alignment.TRAILING,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						107,
																						107)
																				.addComponent(
																						controlBtn,
																						javax.swing.GroupLayout.Alignment.TRAILING,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						107,
																						107)
																				.addComponent(
																						soundComb,
																						0,
																						107,
																						107))))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(browserPanel,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE).addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														browserPanel,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		title,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGap(18, 18,
																		18)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																				.addComponent(
																						jLabel1)
																				.addComponent(
																						intervalComb,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGap(18, 18,
																		18)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																				.addComponent(
																						jLabel2)
																				.addComponent(
																						levelCombo,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGap(18, 18,
																		18)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																				.addComponent(
																						jLabel3)
																				.addComponent(
																						soundComb,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGap(18, 18,
																		18)
																.addComponent(
																		controlBtn)
																.addGap(18, 18,
																		18)
																.addComponent(
																		updateBtn)))
								.addContainerGap()));

		getContentPane().setBackground(Color.WHITE);
		pack();
		setViewData();
		setViewMode(ENABLE);
	}

	public MainFrame() {
		initComponents();
		initView();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private static void setLookAndFeel() {

		LookAndFeelInfo[] lfs = UIManager.getInstalledLookAndFeels();
		String lf = "";
		for (int i = 0; i < lfs.length; i++) {
			if (lfs[i].getClassName().contains("nimbus")) {
				lf = lfs[i].getClassName();
			}
		}

		if (lf.length() > 0) {
			try {
				UIManager.setLookAndFeel((LookAndFeel) Class.forName(lf)
						.newInstance());
			} catch (Exception e1) {
				e1.printStackTrace();
			} 
		}
	}

	public static void main(String[] args) {
		setLookAndFeel();
		Image img = Toolkit.getDefaultToolkit().getImage("pic/logo.jpg");
		MainFrame frame = new MainFrame();
		frame.setIconImage(img);
		frame.setVisible(true);
		frame.setTitle("赤い羽根");
	}

	private AppData getViewData() {
		return new AppData(intervalComb.getSelectedIndex(),
				levelCombo.getSelectedIndex(), soundComb.getSelectedIndex());
	}

	private void setViewData() {
		AppData data = AppDataManager.getAppdata();
		intervalComb.setSelectedIndex(data.getInterval());
		levelCombo.setSelectedIndex(data.getLevel());
		soundComb.setSelectedIndex(data.getAlerm());
	}

	/**
	 * Setting Select Combo Mode Active or not
	 * 
	 * @param mode
	 */
	private void setViewMode(boolean mode) {
		intervalComb.setEnabled(mode);
		levelCombo.setEnabled(mode);
		soundComb.setEnabled(mode);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		String cmd = e.getActionCommand();

		if (UPDATE.equals(cmd)) {
			browserPanel.setPage(BrowserPanel.DEFAULT_URL);
		} else if (START.equals(cmd)) {
			if (null == svc || svc.isShutdown()) {
				svc = Executors.newSingleThreadScheduledExecutor();
			}
			AppData data = getViewData();
			setViewMode(!ENABLE);
			mgr = new RedFeatherManager(data, browserPanel);
			sche = svc.scheduleAtFixedRate(mgr, 0,
					getInterval(data.getInterval()), TimeUnit.MILLISECONDS);
			controlBtn.setText("終了");
			controlBtn.setActionCommand(STOP);
			AppDataManager.saveAppData(data);

		} else if (STOP.equals(cmd)) {
			sche.cancel(true);
			svc.shutdown();
			setViewMode(ENABLE);
			controlBtn.setText("開始");
			controlBtn.setActionCommand(START);
		}
	}

	public static int getInterval(int interval) {
		if (interval == 0) {
			return 30 * 1000;
		} else if (interval == 1) {
			return 60 * 1000;
		}
		return 60 * 3 * 1000;
	}

	private ScheduledExecutorService svc;
	private ScheduledFuture<?> sche;
	private RedFeatherManager mgr;
	private BrowserPanel browserPanel;
	private JButton controlBtn;
	private JComboBox intervalComb;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JComboBox levelCombo;
	private JComboBox soundComb;
	private JLabel title;
	private JButton updateBtn;

}
